<?php

namespace Biz\pockets\serviceTrait;

use Doraemon\cache\Queue as cQueue;
use Doraemon\amqp\Queue as aQueue;
use Doraemon\tools\HhzEnv;
use Doraemon\tools\Log;
use Doraemon\service\DelayedTask;

/**
 * 为service提供延迟任务
 *
 * @author lining
 *
 */
trait _Delayed
{
    private static function _delayed()
    {
        $debugBackTrace = debug_backtrace(0, 2);
        $args = $debugBackTrace[0]["args"];
        $delayedFunction = "_delayed_" . $debugBackTrace[1]["function"];
        $atTime = array_shift($args);
        if (method_exists(__CLASS__, $delayedFunction)) {
            $callback = [
                __CLASS__,
                $delayedFunction
            ];

            //判断是否异步执行
            if (isset($_SERVER['MODULE_NAME']) && $_SERVER['MODULE_NAME'] == "Queue") {
                call_user_func_array($callback, $args);
                return -1;
            } else {
                $result = DelayedTask::addSyncService($callback, $args, $atTime);

                return $result;
            }
        } else {
            throw new \Exception("delayed function not define");
        }
    }
}

